package com.example.campuslink.ui.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.SecondHandTabAdapter
import com.example.campuslink.commen.adpter.ShopInvitationAdapter
import com.example.campuslink.commen.enums.SecondHandEnum
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.commen.tools.SpacesItemEndDecoration
import com.example.campuslink.databinding.FragmentShopBinding
import com.example.campuslink.viewModel.ShopViewModel

class ShopFragment : BaseFragment<FragmentShopBinding, ShopViewModel>(
    ShopViewModel::class.java,
    R.layout.fragment_shop,
    BR.viewModel
) {
    private var secondHandTabAdapter: SecondHandTabAdapter? = null
    private var secondHandList = SecondHandEnum.getAllLabels()
    private var shopAdapter: ShopInvitationAdapter? = null
    override fun onClick() {
        mViewDataBinding?.freshLayout?.setOnRefreshListener {
            mViewModel?.getInvitation(secondHandList[mViewModel?.tabIndex?.value ?: 0])
            mViewDataBinding?.freshLayout?.isRefreshing = false
        }
    }

    override fun initViews() {
        mViewDataBinding?.rvTab?.adapter = secondHandTabAdapter
        mViewDataBinding?.rvTab?.layoutManager = LinearLayoutManager(requireContext())
        mViewDataBinding?.rvTab?.addItemDecoration(SpacesItemButtonDecoration(15))
        mViewDataBinding?.rvGoods?.adapter = shopAdapter
        mViewDataBinding?.rvGoods?.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        mViewDataBinding?.rvGoods?.addItemDecoration(SpacesItemEndDecoration(30))
        mViewModel?.secondInvitationList?.observe(this) {
            shopAdapter?.setListData(it.reversed())
        }

    }

    override fun initData() {
        mViewModel?.context = requireContext()
        mViewModel?.initDB(requireContext())
        mViewModel?.getInvitation(secondHandList[0])
        secondHandTabAdapter = SecondHandTabAdapter(mViewModel!!)
        secondHandTabAdapter?.setListData(secondHandList)
        shopAdapter = ShopInvitationAdapter(mViewModel!!)
    }
}